package com.karst.essentials.chapter07.galaxyExample
{
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import org.aswing.AsWingManager;

	internal class UserInputHandler
	{
		public static var mouseDown : Boolean = false ;
		
		public static var keyLeft : Boolean = false ;
		public static var keyRight : Boolean = false ;
		public static var keyForward : Boolean = false ;
		public static var keyBackward : Boolean = false ;
		
		public static var camMode:String;
		public static var randomCamActive:Boolean;

		public function UserInputHandler( $stage : Stage = null )
		{
			if( $stage )
			{
				$stage.addEventListener( KeyboardEvent.KEY_UP, keyUpHandler ) ;
				$stage.addEventListener( KeyboardEvent.KEY_DOWN, keyDownHandler ) ;
				$stage.addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler ) ;
				$stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler ) ;
			}
		}
		
		private function keyDownHandler( $event : KeyboardEvent ) : void
		{
			switch( $event.keyCode )
			{
				case Keyboard.UP :
				case "W".charAt() :
					keyForward = true ;
					keyBackward = false ;
					break ;
				case Keyboard.DOWN :
				case "D".charAt() :
					keyForward = false ;
					keyBackward = true ;
					break ;
				case Keyboard.LEFT :
				case "A".charAt() :
					keyLeft = true ;
					keyRight = false ;
					break ;
				case Keyboard.RIGHT :
				case "F".charAt() :
					keyLeft = false ;
					keyRight = true ;
					break ;
				default :
					break ;
				
				
				
				case "1".charCodeAt():
					UserInputHandler.camMode = "thirdPerson";
					break;
				
				case "2".charCodeAt():
					UserInputHandler.camMode = "firstPerson";
					break;
				
				case "3".charCodeAt():
					UserInputHandler.camMode = "randomCam";
					break;
				
				case "4".charCodeAt():
					UserInputHandler.camMode = "defaultCam";
					break;
			}
		}
		
		private function keyUpHandler( $event : KeyboardEvent ) : void
		{
			switch( $event.keyCode )
			{
				case Keyboard.UP :
				case "W".charAt() :
					keyForward = false ;
					break ;
				case Keyboard.DOWN :
				case "D".charAt() :
					keyBackward = false ;
					break ;
				case Keyboard.LEFT :
				case "A".charAt() :
					keyLeft = false ;
					break ;
				case Keyboard.RIGHT :
				case "F".charAt() :
					keyRight = false ;
					break ;
				
				case "3".charCodeAt():
					randomCamActive = true;
					break; 
				
				default :
					break ;
			}
		}
		
		private function mouseUpHandler( $event : MouseEvent ) : void
		{
			mouseDown = false ;
		}

		private function mouseDownHandler( $event : MouseEvent ) : void
		{
			mouseDown = true ;
		}
	}
}